var vm = new Vue({
	el: '#app',
	data: function() {
		function isYongHuMing(s) {
			return /^[\w\u4e00-\u9fa5]{2,20}$/g.test(s);
		}

		function isMiMa(s) {
			return /^\w{6,16}$/g.test(s);
		}

		function validateYongHuMing(rule, value, callback) {
			if(isYongHuMing(value)) {
				callback();
			} else {
				callback(new Error('用户名由中文数字字母或下划线组成'));
			}
		}

		function validateMiMa(rule, value, callback) {
			if(isMiMa(value)) {
				callback();
			} else {
				callback(new Error('密码由数字字母或下划线组成'));
			}
		}

		return {
			yongHu: {
				yongHuMing: '',
				miMa: ''
			},
			rules: {
				yongHuMing: [{
						required: true,
						message: '请输入用户名',
						trigger: 'blur'
					},
					{
						min: 2,
						max: 20,
						message: '长度在 2到 20个字符',
						trigger: 'blur'
					},
					{
						validator: validateYongHuMing,
						trigger: 'blur'
					}
				],
				miMa: [{
						required: true,
						message: '请输入密码',
						trigger: 'blur'
					},
					{
						min: 6,
						max: 16,
						message: '长度在 6到 16个字符',
						trigger: 'blur'
					},
					{
						validator: validateMiMa,
						trigger: 'blur'
					}
				]
			}
		};
	},
	methods: {
		submitForm: function(formName) {
			var _this = this;
			this.$refs[formName].validate(function(valid) {
				if(!valid) {
					console.log('error submit!!');
					return false;
				}
				sessionStorage.setItem('yongHuMing', _this.yongHu.yongHuMing);
				location.href = CONST_STATIC_URL + '/hou-tai/ni-hao.html';
			});
		},
		resetForm: function(formName) {
			this.$refs[formName].resetFields();
		}
	}
});